package com.itheima.stock.controller;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.itheima.stock.anno.MyAnno;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.SysUserService;
import com.itheima.stock.utils.RandomUtil;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.req.UsersPageReqVo;

import com.itheima.stock.vo.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@CrossOrigin
@RestController
@Api
@Slf4j
@RequestMapping(path = "/api")
public class UserController {
    @Autowired
    SysUserService sysUserService;

    @Autowired
    StringRedisTemplate redis;


    @PostMapping("/users")
    public R<PageVo<UsersPageRespVo>> getPageToUser(@RequestBody UsersPageReqVo vo){
        System.out.println(vo);
       PageVo<UsersPageRespVo> page =  sysUserService.getUserPage(vo);



        return R.ok("1",page);
    }

    /**
     * 登录接口
     * @param vo
     * @return
     */

    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo) {
        System.out.println(vo);
        log.info("调用了登录接口");
        return sysUserService.login(vo);

    }

    /**
     * 发送验证码
     *
     * @param vo
     * @return
     */
    @ApiOperation(value = "获取验证码接口")
    @MyAnno()
    @GetMapping("/captcha")
    public R<GetCodeVo> getCode(HttpServletRequest request) {
        //工具类生成验证码
        String fourBitRandom = RandomUtil.getFourBitRandom();
        log.info("生成了验证码:"+fourBitRandom);
        //根据key - value存入redis
        String rkey = "api:captcha:code:" + fourBitRandom;
        redis.opsForValue().set(rkey, fourBitRandom);
        //返回结果集
        System.out.println("aaaaaa");
        return R.ok(new GetCodeVo(fourBitRandom, rkey));
    }



    /**
     * 测试接口
     * @return
     */
    @ApiOperation(value = "测试接口")
    @GetMapping(path = "/test")
    public String getName() {
        return "itheima";
    }


}